package com.lucene.blog.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.lucene.blog.bean.Blog;
import com.lucene.blog.bean.PageBean;
import com.lucene.blog.db.service.BlogService;
import com.lucene.blog.db.service.CommentService;
import com.lucene.blog.lucene.BlogIndex;
import com.lucene.blog.util.DateJsonValueProcessor;
import com.lucene.blog.util.ResponseUtil;
import com.lucene.blog.util.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * 后台博客Controller
 * @author yachao
 *
 */
@Controller
@RequestMapping("/admin/blog")
public class BlogAdminController {
	
	@Resource
	private BlogService blogService;
	@Resource
	private CommentService commentService;
	
	private BlogIndex blogIndex = new BlogIndex();
	
	/**
	 * 添加或修改博客信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public String save(Blog blog, HttpServletResponse response) throws Exception {
		int resultTotal = 0;
		if(blog.getBlogId() == null) {
			resultTotal = blogService.addBlog(blog);
			blogIndex.addIndex(blog);
		} else {
			resultTotal = blogService.updateBlog(blog);
			blogIndex.updateIndex(blog);
		}
		
		JSONObject result = new JSONObject();
		if(resultTotal > 0) {
			result.put("success", true);
		} else {
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 分页查询博客信息
	 * @param page
	 * @param rows
	 * @param s_blog
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/listBlog")
	public String listBlog(@RequestParam(value="page", required=false) String page,
						   @RequestParam(value="rows", required=false) String rows,
						   Blog s_blog,
						   HttpServletResponse response) throws Exception {
		PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("title", StringUtil.formatLike(s_blog.getTitle()));
		condition.put("pageBegin", pageBean.getPageBegin());
		condition.put("pageSize", pageBean.getPageSize());
		
		List<Blog> blogList = blogService.getBlogListWithPaging(condition);
		Long total = blogService.getBlogCount(condition);
		
		JSONObject result = new JSONObject(); 
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(blogList, jsonConfig);
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 删除博客信息
	 * @param ids
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public String deleteBlog(@RequestParam(value="ids", required=false) String ids,
							 HttpServletResponse response) throws Exception {
		String[] idsStr = ids.split(",");
		for(int i = 0; i < idsStr.length; i++) {
			int blogId = Integer.parseInt(idsStr[i]);
			commentService.deleteCommentByBlogId(blogId);
			blogService.deleteBlog(blogId);
			blogIndex.deleteIndex(idsStr[i]);
		}
		JSONObject result = new JSONObject();
		result.put("success", true);
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 根据博客id查询博客信息s
	 * @param id
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findById")
	public String findById(@RequestParam(value="blogId", required=false) String blogId,
						   HttpServletResponse response) throws Exception {
		Blog blog = blogService.findBlogById(Integer.parseInt(blogId));
		JSONObject result = JSONObject.fromObject(blog);
		ResponseUtil.write(response, result);
		return null;
	}
}
